package com.ScoresSystem.controller;

import com.ScoresSystem.utils.Result;
import com.ScoresSystem.pojo.Student;
import com.ScoresSystem.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    // 分页查询所有学生
    @RequestMapping("/getAllStudent")
    // @ResponseBody   // 返回数据自动转为json数据
    // @RequestParam 获取前端参数，赋给形参，@RequestParam中的 value必须与前端参数名一致
    public Result<List<Student>> getAllStudent(@RequestParam (value = "studentName", defaultValue = "") String name,
                                              @RequestParam (value = "page", defaultValue = "1") int page,
                                              @RequestParam (value = "limit", defaultValue = "10") int limit){
        List<Student> studentList = studentService.getAllStudent("%" + name + "%", (page - 1) * limit, limit);
        int count = studentService.getCount("%" + name + "%");
        return new Result<>("0", "查询成功", studentList, count);
    }

    // 添加学生
    @RequestMapping("/addStudent")
    public Result<String> addStudent(Student student){
        // 检查学号是否存在
        Student stu1 = studentService.findStudentByStuNumber(student.getStudentNo());
        if (stu1 != null){
            return new Result<>("1", "学号已存在");
        }
        int result = studentService.addStudent(student);
        if (result == 1){
            return new Result<>("0", "添加成功");
        }
        return new Result<>("2", "添加失败");
    }

    // 修改学生信息
    @RequestMapping("/updateStudent")
    public Result<String> updateStudent(Student student){
        int result = studentService.updateStudent(student);
        if (result == 1){
            return new Result<>("0", "修改成功");
        }
        return new Result<>("1", "修改失败");
    }

    // 删除学生
    @RequestMapping("/deleteStudent")
    public Result<String> deleteStudent(@RequestParam (value = "studentNo") String studentNo){
        int result = studentService.deleteStudent(studentNo);
        if (result == 1){
            return new Result<>("0", "删除成功");
        }
        return new Result<>("1", "删除失败");
    }

    // 查询所有学生姓名
    @RequestMapping("/studentSelect")
    public Result<List<Student>> studentSelect(){
        List<Student> studentList = studentService.studentSelect();
        if (studentList == null || studentList.isEmpty()){
            return new Result<>("1", "查询失败");
        }
        return new Result<>("0", "查询成功", studentList);
    }

    // 登陆
    @RequestMapping("/login")
    public Result<Student> login(String userName, String password, String userCode, HttpSession session) {
        // 获取session中的验证码
        String systemCode = (String) session.getAttribute("systemCode");
        if(systemCode.equalsIgnoreCase(userCode)){
            // 验证码正确, 验证用户名和密码
            Student student = studentService.login(userName, password);
            if (student != null) {
                session.setAttribute("nowusername",student.getStudentName());
                session.setAttribute("studentNo",student.getStudentNo());
                return new Result<>("0", "登录成功");
            } else {
                return new Result<>("1", "用户名或密码错误");
            }
        }
        return new Result<>("2", "验证码错误");
    }

    // 修改密码
    @RequestMapping("/updatePassword")
    public Result<String> updatePassword(String oldPassword, String newPassword, HttpSession session) {
        String studentNo = String.valueOf(session.getAttribute("studentNo"));
        //检查老密码是否正确
        Student student = studentService.findStudent(studentNo,oldPassword);
        if (student == null){
            return new Result<>("1", "旧密码错误");
        }
        int result = studentService.updatePassword(studentNo,newPassword);
        if (result > 0) {
            return new Result<>("0", "修改成功");
        }
        return new Result<>("1", "修改失败");
    }

}
